03. Demo: Backtest 2
Part 1
Cd13650 C5 L4 Demo 2 V3
Enhancing the Training Code for Backtesting
Recap and Setup:
- Previous code for data processing and some training functionalities was set up.
- An inverse transform was introduced, crucial for today's backtesting enhancements.
Data Frame for Trade Tracking:
- Initialize an empty Pandas DataFrame with columns:
buy price,buy timestamp,sell price,sell timestamp. - Optional extra columns can be added for advanced metrics.
- Initialize an empty Pandas DataFrame with columns:
Post Analysis Statistics:
- Calculate metrics like maximum drawdown and win-loss ratio using buy and sell prices.
- Additional metrics for filtering include
volume,20-day moving average, and20-day standard deviation.
Backtest Loop Enhancements:
- Use
inverse transformto convert normalized prices back to dollar values. - Adjust for sell price, using proper indexers.
- Ensure all prices are extracted and converted accurately using the
normalizer close.
- Use
Trade Tracking Enhancements:
- Track trades by referencing original datasets for dates, volume, and other indexed metrics.
Graphical Representation:
- Normalize and transform dataset values to plot true values for better interpretation.
- Display winning against losing trades for analysis.
Part 2
Cd13650 C5 L4 Demo 2b V3
Analyzing Backtest Results Part 1
Data Preparation
- Create a
trade profitcolumn in the DataFrame by subtracting the buy price from the sell price. - Introduce
winnerandlosercolumns based on whether thetrade profitis positive or negative.
- Create a
Verification
- Ensure calculations reflect accurate trade results by confirming buy and sell price logic.
Post Analysis Statistics
- Maximum Drawdown: Determine the worst trade by finding the smallest (most negative) value in the
trade profitcolumn. - Win-Loss Ratio: Calculate the ratio of winning to losing trades by counting true values in the
winnerandlosercolumns.
- Maximum Drawdown: Determine the worst trade by finding the smallest (most negative) value in the
Visualizing Results
- Use Seaborn's
histplotfunction to graphically represent the data. - Plot winners and losers based on buy volume, adding aesthetic parameters like transparency and trend lines for better clarity.
- Use Seaborn's
This approach helps efficiently analyze trading strategies, offering insights for optimization.
Part 3
Cd13650 C5 L4 Demo 2c V3
Analyzing Backtest Results Part 2
Chart Analysis:
- Objective: Understand trade charts to apply effective filters for a trading strategy.
- Observations: Current charts lack clear separation between winners and losers, making it challenging to determine effective filters.
- Data Insight: Both winning and losing trades show similar trends, requiring deeper analysis to identify filters.
Filtering Trade Data:
- Volume Analysis: Attempt to filter trades based on volume was insufficient, with only two trades labeled as winners.
- MA 20 and Standard Deviation 20: Explore alternative variables like moving average and standard deviation to create potential filters.
Recommendations for Filter Application:
- Identify Patterns: Look for distinct peaks or valleys in trade charts that indicate a larger number of winners or losers.
- Adjust Test Period: Always apply potential filters to a different time period to validate effectiveness and avoid bias.
- Iterate and Test: Continuously refine strategies and test with new data to ensure robust trading strategies.
Course Objectives:
- Focus on reinforcement learning for automated trading bots with emphasis on risk limitation.
- Encourage learners to add stop losses and take profits as part of a comprehensive trading strategy.